package com.dobby.pea.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dobby.pea.common.BaseContext;
import com.dobby.pea.common.R;
import com.dobby.pea.entity.tbAddressBook;
import com.dobby.pea.entity.tbGood;
import com.dobby.pea.entity.tbUser;
import com.dobby.pea.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Dobby
 * @version 1.0
 */

@RestController
@RequestMapping("/api/addressbook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody tbAddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("地址为:{}", addressBook);
        tbAddressBook addressBookDefaulAddressBook = addressBookService.DefaultAddressBook(addressBook.getUserId());
        if (addressBookDefaulAddressBook==null) addressBook.setIsDefault(1);
        addressBookService.save(addressBook);
        return R.success("新增地址成功");

    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("default")
    public R<tbAddressBook> setDefault(@RequestBody tbAddressBook addressBook) {
        log.info("设置默认地址addressBook:{}", addressBook);
        LambdaUpdateWrapper<tbAddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(tbAddressBook::getUserId, BaseContext.getCurrentId());
        wrapper.set(tbAddressBook::getIsDefault, 0);//把所有地址消除默认地址
        addressBookService.update(wrapper);
        addressBook.setIsDefault(1);//修改改地址为默认地址
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    /**
     * 根据id查询地址(单条数据查询）
     */
    @GetMapping("/{id}")
    public R get(@PathVariable Long id) {
        log.info("根据id查询地址addressBook:{}", id);
        tbAddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return R.success(addressBook);
        } else {
            return R.error("没有找到该地址");
        }
    }

    /**
     * 查询默认地址
     */
    @GetMapping("default")
    public R<tbAddressBook> getDefault() {
        LambdaQueryWrapper<tbAddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(tbAddressBook::getUserId, BaseContext.getCurrentId());
        queryWrapper.eq(tbAddressBook::getIsDefault, 1);
        tbAddressBook addressBook = addressBookService.getOne(queryWrapper);

        if (null == addressBook) {
            return R.error("没有找到该地址");
        } else {
            return R.success(addressBook);
        }
    }

    /**
     * 查询指定用户的全部地址
     * @param addressBook
     * @return
     */
    @GetMapping("/list")
    public R<List<tbAddressBook>> list(tbAddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("addressBook:{}", addressBook);

        LambdaQueryWrapper<tbAddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != addressBook.getUserId(), tbAddressBook::getUserId, addressBook.getUserId());
        queryWrapper.orderByDesc(tbAddressBook::getUpdateTime);

        List<tbAddressBook> list = addressBookService.list(queryWrapper);

        if (list==null){
            return R.error("地址为空");
        }else {
            return R.success(list);
        }
    }

    /**
     * 根据地址id更新地址
     * @param addressBook
     * @return
     */
    @PutMapping()
    public R<tbAddressBook> getById(@RequestBody tbAddressBook addressBook){
        log.info("根据地址id更新地址...");
        LambdaQueryWrapper<tbAddressBook> queryWrappergetId = new LambdaQueryWrapper<>();
        queryWrappergetId.eq(tbAddressBook::getId,addressBook.getId());
        int count = addressBookService.count(queryWrappergetId);


        if(count <=0){
            return R.error("没有找到对应的地址无法更新");
        }

        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    /**
     * 根据地址id删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);

        LambdaQueryWrapper<tbAddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(tbAddressBook::getId,ids);

        UpdateWrapper<tbAddressBook> UpdateWrapper = new UpdateWrapper<>();
        UpdateWrapper.set("is_Deleted",1);
        UpdateWrapper.in("id",ids);


        int count = addressBookService.count(queryWrapper);

        if (count<=0){
            return R.error("地址不存在无法删除");
        }else{
            addressBookService.update(UpdateWrapper);
            return R.success("地址删除成功");
        }

    }




}
